import Request from 'utils/request';

import CouponForm from 'models/couponMgr/couponForm';

export default class CouponMgrApi {
  static getCouponList(search, pagination) {
    const apiName = 'coupon/branchQueryCoupons';

    let postData = search.getObject(pagination);

    return Request.post(apiName, postData).then(res => {
      let coupons = res.data;
      let couponList = [];
      for (let i = 0; i < coupons.length; i++) {
        let coupon = coupons[i];
        coupon = new CouponForm(coupon);
        couponList.push(coupon);
      }

      pagination.totalCount = res.rows;
      return couponList;
    });
  }

  static addGoupon(couponForm) {
    const apiName = 'coupon/branchCreateCoupons';

    let postData = couponForm.getObject();

    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static updateGouponStatus(stockId, status) {
    const apiName = 'coupon/branchStopCoupons';

    return Request.post(apiName, {
      stockId,
      stopOrStart: status
    }).then(res => {
      return res.msg;
    });
  }
}
